<?php

namespace app\admin\controller;
use app\common\controller\Admin;
use app\ads\controller\Api;

/**
 * 数据
 *
 * @Author 小邓
 * @DateTime 2022-11-05
 */
class Help extends Admin {

	public function _initialize() {
		parent::_initialize();

    }


    public function _empty(){
        $action = ACTION_NAME;
        //var_dump($action);
        $pageSize = 50;
        $db ='help';
        $map['uid'] =session('user_auth.uid');
        $order = 'id desc';

        $list = db( $db )->where($map)->order( $order )->paginate( $pageSize, false, [ 'type'=> 'page\Page' ,'query' => request()->param()] );
        foreach ($list as $k => $v) {
            if ($v['type'] == 'video') {
                $v['content_video'] = $v['content'];
            } else {
                $v['content_image'] = $v['content'];
            }
            $list[$k] = $v;
        }
        $page = $list->render();
        $this->assign(compact('list', 'page'));
        $this->setMeta("新手教程");
        return view('help/'.$action );
        // return view('help/operate_help');
    }

    /**
     * 添加运营教程
     * @author netlife <179597995@qq.com>
     */
    public function add() {
        if (IS_POST) {
            $data = $this->request->param();
            $datain =[];
            if(!array_key_exists('type',$data)){
                return $this->error('文件类型缺失',url('admin/help/operate_help'));
            }
            if ($data['type'] == 'video') {
                if(!array_key_exists('content_video',$data)){
                    return $this->error('视频内容不能为空',url('admin/help/operate_help'));
                }
            } else {
                if(!array_key_exists('content_image',$data)){
                    return $this->error('图片内容不能为空',url('admin/help/operate_help'));
                }
            }
            
            $datain['title']  = $data['title'];
            $datain['content'] = $data['type'] == 'video' ? $data['content_video'] : $data['content_image'];
            $datain['cover'] = isset($data['cover']) ? $data['cover'] : null;
            $datain['type'] = $data['type'];
            $datain['uid'] = session('user_auth.uid');
            $datain['create_time'] = time();
            $datain['update_time'] = time();
            $result = db('help')->insertGetId($datain);

            if ($result) {
                return $this->success('添加成功！', url('admin/help/operate_help'));
            } else {
                return $this->error('添加失败',url('admin/help/operate_help'));
            }

        } else {
            return $this->error('非法提交',url('admin/help/operate_help'));
        }
    }

}